package com.sjzpalh.dss.push.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

import com.sjzpalh.dss.push.primitive.CmdInfo;
import com.sjzpalh.dss.push.primitive.Command;
import com.sjzpalh.dss.push.primitive.ControlCmd;

/**
 * {@link DelegateCodec} implementation.
 * @author
 */
public class ControlCmdCodec implements DelegateCodec {

	 @Override
	    public void encode(ChannelHandlerContext aCtx, Command aMsg, ByteBuf aOut) throws Exception {
		 ControlCmd cmd = (ControlCmd) aMsg;
	        CmdInfo cmdInfo = cmd.getCmdInfo();
	        Record rec = Record.forWrite();
	        rec.addInt(cmd.getId());
	        
	        int cmdType = cmdInfo.getCmdType();
	        rec.addInt(cmdType);
	        
	        String paramValue=cmdInfo.getParamValue();
	        rec.addString(paramValue);
	        
	        rec.addString(cmd.getReslut());
	        
	        rec.write(aOut);
	    }

	    @Override
	    public void decode(ChannelHandlerContext aCtx, ByteBuf aIn, List<Object> aOut) throws Exception {
	        Record rec = Record.read(aIn);
	        int cmdId = rec.getInt();
	        int cmdType=rec.getInt();
	        String paramValue=rec.getString();
	        String result=rec.getString();

	        CmdInfo cmdInfo = new CmdInfo(cmdType, paramValue);
	        ControlCmd cmd = new ControlCmd(cmdId, cmdInfo,result);
	        aOut.add(cmd);
	    }
}
